-
Notifications
You must be signed in to change notification settings - Fork 13.6k
std: lazily allocate the main thread handle #132654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
r? @ChrisDenton rustbot has assigned @ChrisDenton. Use |
This comment has been minimized.
This comment has been minimized.
EDIT: I was right! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be happy with this approach but it would be nice to have some consensus on it to (hopefully) avoid further churn. I won't insist on it though.
library/std/src/thread/mod.rs
Outdated
#[cfg(not(target_thread_local))] | ||
#[allow(unused)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this change is needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this is also used when 64-bit atomics are unavailable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any more specific cfg
that can be used? #[allow(unused)]
, while sometimes necessary, makes me nervous because it could silently become actual dead code in the future.
library/std/src/thread/thread.rs
Outdated
// INVARIANT: must be valid UTF-8 | ||
name: Option<CString>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change seems worse. Having an invariant that can be violated in safe code is not great.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also wondered if it made sense to have a new-type that held both invariants (UTF-8 & c-string \0 ending) ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Utf8CStr is a fairly common type in the ecosystem, it would make sense for us to start tinkering with providing it.
Also could you separate the file move into a different commit? It's easier to diff that way. |
This comment has been minimized.
This comment has been minimized.
☔ The latest upstream changes (presumably #133219) made this pull request unmergeable. Please resolve the merge conflicts. |
I've undone the @rustbot ready |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
☔ The latest upstream changes (presumably #135279) made this pull request unmergeable. Please resolve the merge conflicts. |
☔ The latest upstream changes (presumably #135357) made this pull request unmergeable. Please resolve the merge conflicts. |
This reverts commit 0747f28.
Thereby, we also allow accessing thread::current before main: as the runtime no longer tries to install its own handle, this will no longer trigger an abort. Rather, the name returned from name will only be "main" after the runtime initialization code has run, but I think that is acceptable. This new approach also requires some changes to the signal handling code, as calling `thread::current` would now allocate when called on the main thread, which is not acceptable. I fixed this by adding a new function (`with_current_name`) that performs all the naming logic without allocation or without initializing the thread ID (which could allocate on some platforms).
Originally authored by GnomedDev
@bors r=ChrisDenton |
#123550 eliminated the allocation of the main thread handle, but at the cost of greatly increased complexity. This PR proposes another approach: Instead of creating the main thread handle itself, the runtime simply remembers the thread ID of the main thread. The main thread handle is then only allocated when it is used, using the same lazy-initialization mechanism as for non-runtime use of
thread::current
, and thename
method uses the thread ID to identify the main thread handle and return the correct name ("main") for it.Thereby, we also allow accessing
thread::current
before main: as the runtime no longer tries to install its own handle, this will no longer trigger an abort. Rather, the name returned fromname
will only be "main" after the runtime initialization code has run, but I think that is acceptable.This new approach also requires some changes to the signal handling code, as calling
thread::current
would now allocate when called on the main thread, which is not acceptable. I fixed this by adding a new function (with_current_name
) that performs all the naming logic without allocation or without initializing the thread ID (which could allocate on some platforms).Reverts #123550, CC @GnomedDev